tinymce.PluginManager.add('fullimage', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('fullimage', {
        icon: true,
        image: '/themes/admin/img/editor/fullimage.png',
        title: 'Full width image without ImageBox',
        stateSelector: 'img.fullimage',
        onclick: function() {
            var currentNode = tinyMCE.activeEditor.selection.getNode();
            if(currentNode.nodeName.toLowerCase()=="img"){
                if(!this.active()){
                    currentNode.className = "fullimage";
                }else{
                    currentNode.className = "";
                }
            }
        }
    });
});

tinymce.PluginManager.add('productbox', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('productbox', {
        icon: true,
        image: '/themes/admin/img/editor/amazon.png',
        title: 'Amazon PartnerNet Product Box',
        onclick: function() {
            // Open window
            editor.windowManager.open({
                title: 'Insert Amazon Product',
                body: [
                    {type: 'textbox', name: 'asin', label: 'ASIN'}
                ],
                onsubmit: function(e) {
                    if(e.data.asin!=""){
                        editor.insertContent('<productbox>'+e.data.asin+'</productbox>');
                    }
                }
            });
        }
    });
});

tinymce.PluginManager.add('youtubevideo', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('youtubevideo', {
        icon: true,
        image: '/themes/admin/img/editor/youtube.png',
        title: 'Embedded YouTube Video',
        onclick: function() {
            // Open window
            editor.windowManager.open({
                title: 'Embed YouTube Video',
                body: [
                    {type: 'textbox', name: 'videoid', label: 'Video Id'}
                ],
                onsubmit: function(e) {
                    if(e.data.videoid!=""){
                        editor.insertContent('<youtubevideo>'+e.data.videoid+'</youtubevideo>');
                    }
                }
            });
        }
    });
});

tinymce.PluginManager.add('codeblock', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('codeblock', {
        icon: true,
        image: '/themes/admin/img/editor/code.png',
        title: 'Syntax highlighted program code',
        onclick: function() {
            var isExistCode = false;
            var existCodeText = "";
            var existCodeLang = "";

            // get the actively selected node and check if its code
            var currentNode = tinyMCE.activeEditor.selection.getNode();
            if(currentNode.nodeName.toLowerCase()=="code"){
                // we will load this code into the editor
                // and have it edited instead of creating new code
                isExistCode = true;
                existCodeText = atob(currentNode.innerText);
                existCodeLang = $(currentNode).attr("lang");
            }

            // open the window with CodeMirror
            editor.windowManager.open({
                title: 'Programming with syntax highlighted code',
                url: '/themes/admin/dialog/codeblock.html',
                width:640,
                height:480,
                buttons: [{
                    text: 'Save',
                    classes: 'widget btn primary first abs-layout-item',
                    onclick: function(){
                        var codeLang = $(".mce-container iframe").contents().find("#codeblocklang").val();
                        var codeText = $(".mce-container iframe").contents().find("#codeeditor").val();
                        
                        /* update the content when the selector is in the code element
                            and if not, then insert a new code element */
                        if(isExistCode){
                            $(currentNode).html(btoa(codeText));
                            $(currentNode).attr("lang",codeLang);
                        }else{
                            editor.insertContent('<code lang="'+codeLang+'">'+btoa(codeText)+'</code>');
                        }

                        editor.windowManager.close();
                    }
                },{
                    text: 'Close',
                    onclick: 'close'
                }]
            },{
                isExistingCode: isExistCode,
                existingCodeText: existCodeText,
                existingCodeLang: existCodeLang   
            });
        }
    });
});